/*
@author: xyl
@file: atm.go
@time: 2022/12/2 14:46
*/
package main

import (
	"fmt"
)

type User struct {
	name    string
	passwd  string
	balance int
}

type ATM struct {
	bank    string
	balance int
}

func (atm *ATM) saveMoney(user *User, money int) {
	user.balance += money
	atm.balance += money
	fmt.Println("存入成功！您的余额为：", user.balance)
}
func (atm *ATM) drawMoney(user *User, money int) {
	if user.balance < money {
		fmt.Println("账户余额不足")
	} else if atm.balance < money {
		fmt.Println("ATM余额不足")
	} else {
		user.balance -= money
		atm.balance -= money
		fmt.Println("取款成功！余额为：", user.balance)
	}
}
func (atm *ATM) checkMoney(user *User) {
	fmt.Println("您的余额为：", user.balance)
}

var users = map[string]*User{}

func main() {
	atm := ATM{bank: "农业银行", balance: 10000000}
	flag := true
	for flag {
		flag = false
		var op string
		fmt.Println("请输入你的选择(按q退出):\n1、登录\n2、注册")
		fmt.Scan(&op)
		switch op {
		case "q":
			flag = false
		case "1":
			var name string
			var passwd string
			fmt.Println("请输入用户名：")
			fmt.Scan(&name)
			fmt.Println("请输入密码：")
			fmt.Scan(&passwd)
			_, ok := users[name]
			if ok && passwd == users[name].passwd {
				fmt.Println("登录成功！")
				restart := true
				for restart {
					restart = false
					var option string
					fmt.Println("请输入你的选择:\n1、存款\n2、取款\n3、查询余额\n4、退出")
					fmt.Scan(&option)
					switch option {
					case "1":
						var save int
						fmt.Println("请输入存入金额：")
						fmt.Scan(&save)
						atm.saveMoney(users[name], save)
						restart = true
					case "2":
						var draw int
						fmt.Println("请输入取出金额：")
						fmt.Scan(&draw)
						atm.drawMoney(users[name], draw)
						restart = true
					case "3":
						atm.checkMoney(users[name])
						restart = true
					case "4":
						restart = false
					default:
						fmt.Println("输入不合法！")
						restart = true
					}
				}
			} else {
				fmt.Println("用户名或密码错误，请重试！")
			}
			flag = true
		case "2":
			var name string
			var passwd string
			var money int
			fmt.Println("请输入用户名：")
			fmt.Scan(&name)
			fmt.Println("请输入密码：")
			fmt.Scan(&passwd)
			fmt.Println("充值初始金额：")
			fmt.Scan(&money)
			user := User{
				name:    name,
				passwd:  passwd,
				balance: money,
			}
			users[name] = &user
			flag = true
		default:
			fmt.Println("输入不合法！")
			flag = true
		}
	}
}
